package com.ruoyi.exam.service.impl;

import java.util.List;
import java.util.UUID;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.exam.domain.ExamPaperQuestion;
import com.ruoyi.exam.dto.ExamPaperDto;
import com.ruoyi.exam.dto.ExamPaperQuestionDto;
import com.ruoyi.exam.dto.QuestionsDto;
import com.ruoyi.exam.mapper.ExamPaperQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.exam.mapper.ExamPaperMapper;
import com.ruoyi.exam.domain.ExamPaper;
import com.ruoyi.exam.service.IExamPaperService;

/**
 * 试卷信息Service业务层处理
 *
 * @author wangshi
 * @date 2021-10-25
 */
@Service
public class ExamPaperServiceImpl implements IExamPaperService
{
    @Autowired
    private ExamPaperMapper examPaperMapper;
    @Autowired
    private ExamPaperQuestionMapper examPaperQuestionMapper;

    /**
     * 查询试卷信息
     *
     * @param id 试卷信息主键
     * @return 试卷信息
     */
    @Override
    public ExamPaper selectExamPaperById(String id)
    {
        return examPaperMapper.selectExamPaperById(id);
    }

    /**
     * 查询试卷信息列表
     *
     * @param examPaper 试卷信息
     * @return 试卷信息
     */
    @Override
    public List<ExamPaper> selectExamPaperList(ExamPaper examPaper)
    {
        return examPaperMapper.selectExamPaperList(examPaper);
    }

    /**
     * 新增试卷信息
     *
     * @param ExamPaperDto 试卷信息
     * @return 结果
     */
    @Override
    public int insertExamPaper(ExamPaperDto examPaperDto)
    {
        ExamPaper examPaper = new ExamPaper();
//        dto 转换成 ExamPaper 实体
        examPaper.setPaperName(examPaperDto.getPaperName());
        examPaper.setPaperinfo(examPaperDto.getPaperinfo());
        examPaper.setSemester(examPaperDto.getSemester());
        examPaper.setSubjectId(examPaperDto.getSubjectId());
        examPaper.setId(UUID.randomUUID().toString().replaceAll("-",""));
        examPaper.setCreateTime(DateUtils.getNowDate());
        int row = examPaperMapper.insertExamPaper(examPaper);
        if(row < 0){
            return 0;
        }
//        dto 转换多对多的关联
        List<ExamPaperQuestionDto> examPaperQuestionDtos = examPaperDto.getExamPaperQuestionDtos();
        ExamPaperQuestion examPaperQuestion = new ExamPaperQuestion();
//        多对多 逐个添加
        for (ExamPaperQuestionDto examPaperQuestionItem :
                examPaperQuestionDtos) {
            examPaperQuestion.setId(UUID.randomUUID().toString().replaceAll("-",""));
            examPaperQuestion.setCreateTime(DateUtils.getNowDate());
            examPaperQuestion.setQuestionName(examPaperQuestionItem.getQuestionType());
            List<QuestionsDto> questionsList = examPaperQuestionItem.getQuestionsList();
            int len = questionsList.size();
            for (int i = 0; i < len; i++) {
                QuestionsDto questionsDto = questionsList.get(i);
                examPaperQuestion.setQuestionId(questionsDto.getId());
                examPaperQuestion.setExamPaperId(examPaper.getId());
                examPaperQuestionMapper.insertExamPaperQuestion(examPaperQuestion);
            }
        }

        return row;
    }

    /**
     * 修改试卷信息
     *
     * @param examPaper 试卷信息
     * @return 结果
     */
    @Override
    public int updateExamPaper(ExamPaper examPaper)
    {
        examPaper.setUpdateTime(DateUtils.getNowDate());
        return examPaperMapper.updateExamPaper(examPaper);
    }

    /**
     * 批量删除试卷信息
     *
     * @param ids 需要删除的试卷信息主键
     * @return 结果
     */
    @Override
    public int deleteExamPaperByIds(String[] ids)
    {
        return examPaperMapper.deleteExamPaperByIds(ids);
    }

    /**
     * 删除试卷信息信息
     *
     * @param id 试卷信息主键
     * @return 结果
     */
    @Override
    public int deleteExamPaperById(String id)
    {
        return examPaperMapper.deleteExamPaperById(id);
    }
}
